const query = require('@/modules/database')

module.exports.count = () => {
  let _sql = 'SELECT count(id) as count FROM tag';
  return query(_sql)
}

module.exports.findAll = (req) => {
  let _sql = `SELECT * FROM tag WHERE title LIKE '%${req.params.title || ''}%'`;
  return query(_sql)
}

module.exports.findActive = (req) => {
  let _sql = `SELECT * FROM tag WHERE title LIKE '%${req.params.title || ''}%' AND status=1`;
  return query(_sql)
}

module.exports.detail = (req) => {
  let _sql = `SELECT * FROM tag WHERE id=${req.query.id}`;
  return query(_sql)
}

module.exports.insert = (req) => {
  const params = req.body;
  let _sql = `INSERT INTO tag SET title=${params.title}`
  params.desc && (_sql += `,desc=${params.desc}`)
  params.icon && (_sql += `,icon=${params.icon}`)
  params.color && (_sql += `,color=${params.color}`)
  params.status && (_sql += `,status=${params.status}`)
  return query(_sql)
}

module.exports.update = (req) => {
  const params = req.body;
  let _sql = `UPDATE tag SET title=${params.title}`
  params.desc && (_sql += `,desc=${params.desc}`)
  params.icon && (_sql += `,icon=${params.icon}`)
  params.color && (_sql += `,color=${params.color}`)
  params.status && (_sql += `,status=${params.status}`)
  _sql += `WHERE id=${params.id}`
  return query(_sql)
}

module.exports.remove = (req) => {
  let _sql = `DELETE FROM tag WHERE id = ${req.body.id}`
  return query(_sql)
}